home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / mindos11.zip / MSHDIR.C < prev    next >
C/C++ Source or Header  |  1991-03-22  |  3KB  |  107 lines

  1. /*  mshdir.c    -- show a Minix file directory */
  2. /*  Copyright 1988,1991 Steven W. Harrold - All rights reserved. */
  3. /*  $Header: MSHDIR.C_V 1.4 91/03/19 13:50:20 SWH Exp $ */
  4.  
  5. #include    <stdio.h>
  6. #include    <stdlib.h>
  7. #include    <string.h>
  8. #include    "mfs.h"
  9. #include    "dev.h"
  10.  
  11. READ_FILE
  12. SHOW_ENT
  13. SHOW_NAME
  14.  
  15. extern BOOL     Nonly ;
  16.  
  17.  
  18. /*==================================================================*/
  19. PRIVATE int dcompare (elem1, elem2)
  20. struct directory    *elem1 ;
  21. struct directory    *elem2 ;
  22. {
  23.     return (strncmp(elem1->d_name, elem2->d_name, NAME_SIZE)) ;
  24.  
  25. } /* dcompare() */
  26.  
  27.  
  28. /*==================================================================*/
  29. void show_dir (ddata, iarea, ip, path)
  30. struct devdata  *ddata ;
  31. struct inode    *iarea ;
  32. struct inode    *ip ;
  33. char            *path ;
  34. {
  35.     struct directory    *darea, *dp ;
  36.     int                 dn, dnz ;
  37.  
  38.     int         i, j, k, n ;
  39.     char        c, *p ;
  40.     char        npath[MAX_PATH+1] ;
  41.  
  42.  
  43.     dn = ip->i_size / sizeof(struct directory) ;
  44.     dnz = dn * sizeof(struct directory) + BLOCK_SIZE - 1 ;
  45.     dnz /= BLOCK_SIZE ;
  46.     dnz *= BLOCK_SIZE ;
  47.     dnz /= sizeof(struct directory) ;
  48.     darea = calloc (dnz+1, sizeof(struct directory)) ;
  49.     if (!darea)
  50.         exit(3) ;
  51.  
  52.     read_file (darea, ip, ddata) ;
  53.  
  54. /*  Sort the directory entries by filename
  55.  */
  56.     qsort (darea, dn, sizeof(struct directory), dcompare) ;
  57.  
  58. /*  First show the directory contents
  59.  */
  60.     if (!Nonly)
  61.         printf("Directory: %s\n", path) ;
  62.  
  63.     dp = darea ;
  64.     for (i=0; i<dn; i++, dp++)
  65.     {
  66.         if (dp->d_inum)
  67.         {
  68.             if (Nonly)
  69.                 show_name(path, dp, iarea) ;
  70.             else
  71.                 show_ent (iarea, dp) ;
  72.         }
  73.  
  74.     }
  75.     if (!Nonly)
  76.         printf ("\n") ;
  77.  
  78. /*  Now, for those entries that are themselves directories,
  79.  *  display them recursively.
  80.  */
  81.     dp = darea + 2 ;            /* skip over . and .. entries */
  82.     for (i=2; i<dn; i++, dp++)
  83.     {
  84.         if (n = dp->d_inum) /*assignok*/
  85.             if ((iarea[n].i_mode & I_TYPE) == I_DIRECTORY)
  86.             {
  87.                 strcpy(npath, path) ;
  88.                 if (npath[1])
  89.                     strcat(npath, "/") ;
  90.                 p = npath + strlen(npath) ;
  91.                 for (j=0; (j<NAME_SIZE) && (c=dp->d_name[j]);
  92.                           j++, p++) /*assignok*/
  93.                     *p = c ;
  94.                 *p = '\0' ;
  95.                 show_dir(ddata, iarea, &iarea[n], npath) ;
  96.             }
  97.     }
  98.  
  99. /*  Clean up
  100.  */
  101.     free (darea) ;
  102.  
  103. } /* show_dir() */
  104.  
  105.  
  106. /*---eof---*/
  107.